﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

namespace Songhay.OpenXml.Transformations
{
    using Songhay.Extensions;
    using Songhay.OpenXml;
    using Songhay.OpenXml.Extensions;
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Transforms the Open XML WordML <c>sdt</c> element.
    /// </summary>
    [Export(typeof(IWordProcessingDocumentTransformation))]
    public class ContentControlForCssBlockTransformation : IWordProcessingDocumentTransformation
    {
        /// <summary>
        /// Initializes a new instance
        /// of the <see cref="ContentControlForCssBlockTransformation"/> class.
        /// </summary>
        public ContentControlForCssBlockTransformation()
        {
            this.ExecutionOrdinal = 1;
        }

        #region IWordProcessingDocumentTransformation Members

        /// <summary>
        /// Gets or sets the execution ordinal.
        /// </summary>
        /// <value>The execution ordinal.</value>
        public byte ExecutionOrdinal { get; set; }

        /// <summary>
        /// Determines whether this instance can transform the specified word doc.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can transform the specified word doc; otherwise, <c>false</c>.
        /// </returns>
        public bool CanTransform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node)
        {
            var element = node.GetElement(SonghayW.sdt);
            if(element == null) return false;
            return element.HasSdtTagName("rxCssBlock");
        }

        /// <summary>
        /// Transforms the specified Word document.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <param name="imageHandler">The image handler.</param>
        public object Transform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node,
            Func<ImageInfo, XElement> imageHandler)
        {
            var sdt = node.GetElement(SonghayW.sdt);
            var cssClass = sdt.GetSdtPropertyValue(SonghayW.alias);
            cssClass = cssClass.Replace("div.", string.Empty);

            var div = new XElement(SonghayXhtml.div,
                new XAttribute(SonghayXhtml.@class, cssClass),
                sdt.Element(SonghayW.sdtContent)
                    .Elements()
                    .Select(e =>
                    OpenXmlUtility.TransformPackageNode(
                    wordDoc, settings, e, imageHandler)));

            return div;
        }

        #endregion
    }
}
